package com.gilight.base.controller;

import com.gilight.base.base.LoginUserInfo;
import com.gilight.base.base.ParameterException;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.common.RoleStatus;
import com.gilight.base.dto.RoleDto;
import com.gilight.base.dto.SysUserRoleDto;
import com.gilight.base.model.SysRole;
import com.gilight.base.model.SysUser;
import com.gilight.base.service.SysRoleService;
import com.gilight.base.util.ShiroUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/web/sysRole")
public class SysRoleController {
    @Resource
    private SysRoleService roleService;


    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    private ResultEntity findList(@RequestBody @Validated RoleDto roleDto) {
        ResultEntity resultEntity = new ResultEntity();
        PageInfo page = roleService.findList(roleDto);
        resultEntity.setData(page);
        return resultEntity;
    }

    @RequestMapping(value = "/findRoleList", method = RequestMethod.GET)
    private ResultEntity findRoleList() {
        ResultEntity resultEntity = new ResultEntity();
        List<SysRole> roleList= roleService.findRoleList();
        resultEntity.setData(roleList);
        return resultEntity;
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    private ResultEntity all() {
        ResultEntity resultEntity = new ResultEntity();
        List<SysRole> list = roleService.all();
        resultEntity.setData(list);
        return resultEntity;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultEntity add(@RequestBody SysRole role) {
        ResultEntity resultEntity = new ResultEntity();
        roleService.add(role);
        return resultEntity;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultEntity edit(@RequestBody SysRole role) {
        ResultEntity resultEntity = new ResultEntity();
        roleService.edit(role);
        return resultEntity;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResultEntity delete(@RequestParam Long id) {
        ResultEntity resultEntity = new ResultEntity();
        roleService.delete(id);
        return resultEntity;
    }

    @RequestMapping(value = "/enable", method = RequestMethod.GET)
    public ResultEntity enable(@RequestParam Long id) {
        ResultEntity resultEntity = new ResultEntity();
        roleService.updateStatus(id, RoleStatus.ENABLE);
        return resultEntity;
    }

    @RequestMapping(value = "/disable", method = RequestMethod.GET)
    public ResultEntity disable(@RequestParam Long id) {
        ResultEntity resultEntity = new ResultEntity();
        roleService.updateStatus(id, RoleStatus.DISABLE);
        return resultEntity;
    }

    @RequestMapping(value = "/findAllPermission", method = RequestMethod.GET)
    public ResultEntity findAllPermission(@RequestParam Long id,@RequestParam String appKey,@RequestParam(required = false) Integer type) {
        ResultEntity resultEntity = new ResultEntity();
        List list = new ArrayList();
        if(type!=null&&type==2){
            list =roleService.findAllButtonPermission(id);
        }else{
            list =  roleService.findAllPermission(id,appKey);
        }
        resultEntity.setData(list);
        return resultEntity;
    }
    @RequestMapping(value = "/findAllButtonPermission", method = RequestMethod.GET)
    public ResultEntity findAllButtonPermission() {
        ResultEntity resultEntity = new ResultEntity();
        LoginUserInfo loginUser = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        List list = roleService.findAllButtonPermission( loginUser.getRoleId());
        resultEntity.setData(list);
        return resultEntity;
    }

    @RequestMapping(value = "/assign", method = { RequestMethod.POST, RequestMethod.GET })
    public ResultEntity assign(@RequestParam Long id, @RequestParam String permissionIds,@RequestParam String appKey) {
        ResultEntity resultEntity = new ResultEntity();
        if (StringUtils.isEmpty(permissionIds)) {
            throw new ParameterException("请至少选择一个菜单项！");
        }
        String[] arr = permissionIds.split(",");
        List<Long> permissionIdList = Arrays.stream(arr).map(Long::parseLong).collect(Collectors.toList());
        roleService.assign(id, permissionIdList,appKey);
        return resultEntity;
    }
    @RequestMapping(value = "/assignButton", method = { RequestMethod.POST, RequestMethod.GET })
    public ResultEntity assignButton(Long roleId,Long menuId, String permissionIds) {
        ResultEntity resultEntity = new ResultEntity();
        List<Long> permissionIdList = null;
        if(!permissionIds.trim().equals("")&&permissionIds.trim().length()>0){
            String[] arr = permissionIds.split(",");
            permissionIdList  = Arrays.stream(arr).map(Long::parseLong).collect(Collectors.toList());
        }
        roleService.assignButton(roleId, permissionIdList,menuId);
        return resultEntity;
    }
    /**
     * ��ɫ����˵������ݹ���
     * @param permissionId ��Դ�˵�ID
     * @param dataRuleIds ���ݹ���ID��90,30,40
     * @return
     */
    @RequestMapping(value = "/assignRoleDataRule", method = {RequestMethod.POST, RequestMethod.GET})
    public ResultEntity assignRoleDataRule(Long permissionId,String dataRuleIds) {
        ResultEntity resultEntity = new ResultEntity();
        SysUser sysUser = ShiroUtil.getUser();
        Long roleId = roleService.selectDefaultRoleByUserId(sysUser.getId());
        if (roleId!= null) {
            roleService.assignRoleDataRule(roleId, permissionId,dataRuleIds);
        }
        return resultEntity;
    }

    @RequestMapping(value = "/addUserRoleBatch", method = {RequestMethod.POST, RequestMethod.GET})
    public ResultEntity addUserRoleBatch(@RequestBody Map<String,Object> mapParam) {
        ResultEntity resultEntity = new ResultEntity();
        roleService.addUserRoleBatch(mapParam);
        return resultEntity;
    }
    /**
     * 查询roleId不包含的用户
     * @param startPage
     * @return
     */
    @RequestMapping(value = "/getUsersByRoleId", method = {RequestMethod.POST, RequestMethod.GET})
    public ResultEntity getUsersByRoleId(@RequestParam int startPage, @RequestParam int pageSize,@RequestParam(required = false) String userName, @RequestParam (required = false)String userCode,@RequestParam Long roleId) {
        ResultEntity resultEntity = new ResultEntity();
        Map<String, Object> map = new HashMap<String, Object>();
        PageHelper.startPage(startPage,pageSize);
        map.put("roleId", roleId);
        map.put("userName", userName);
        map.put("userCode", userCode);
        List<Map<String,Object>> userList=roleService.getUserListByRoleId(map);
        PageInfo pageInfo = new PageInfo(userList);
        resultEntity.setData(pageInfo);
        return resultEntity;
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/deleteRole", method = {RequestMethod.POST, RequestMethod.GET})
    public ResultEntity deleteRole(@RequestParam Long roleId) {
        ResultEntity resultEntity = new ResultEntity();
        roleService.deleteRole(roleId);
        return resultEntity;
    }

    /**
     * 删除角色中的用户
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/deleteUserRole", method = { RequestMethod.GET})
    public ResultEntity deleteRole(@RequestParam Long roleId,@RequestParam Long userId) {
        ResultEntity resultEntity = new ResultEntity();
        roleService.deleteUserRole(roleId,userId);
        return resultEntity;
    }
}
